<?php

//Function to make a database connection
function makeConnection() {
    //Setup variable ***CHANGE HERE ONLY***
    
    $host='localhost'; //country 0
    $username='s91199'; //username 0
    $password='123456'; //password 0
    $dbname='s91199'; //name 0
    /*
    $host='localhost'; //country 0
    $username='root'; //username 0
    $password=''; //password 0
    $dbname='mydb'; //name 0*/
    try {
        $connection = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
        return $connection;
    } catch (PDOException $e) {
        print "Error!: " . $e->getMessage() . "<br/>";
        die();
    }
}

/* Function for SELECT statement
 * Return data: ASSOCIATIVE ARRAY for all rows returned
*/

function executeSelectQuery($query) {
    try {
        $dbh = makeConnection();
        $sth = $dbh->prepare($query);
        $sth->execute();
        $result = $sth->fetchAll(PDO::FETCH_ASSOC);
        return $result;
    } catch (PDOException $e) {
        print "Error!: " . $e->getMessage() . "<br/>";
        die();
    }
}

/* Function for DELETE,UPDATE,DELET statement
 * Return data: Number of rows affected
 * NOTE: Cannot use for SELECT statement
*/

function executeQuery($query) {
    try {
        $dbh = makeConnection();
        $count = $dbh->exec($query);
        return $count;
    } catch (PDOException $e) {
        print "Error!: " . $e->getMessage() . "<br/>";
        die();
    }
}

/* Function for COUNT TOTAL COLUMNS
 * Return data: int: Total Columns
*/

function countColumns($query) {
    try {
        $dbh = makeConnection();
        $sth = $dbh->prepare($query);
        $sth->execute();
        $count = $sth->columnCount();
        return $count;
    } catch (PDOException $e) {
        print "Error!: " . $e->getMessage() . "<br/>";
        die();
    }
}

/* Function for COUNT TOTAL ROWS
 * Return data: int: Total Rows
*/
function countRows($query) {
    try {
        $dbh = makeConnection();
        $sth = $dbh->prepare($query);
        $sth->execute();
        $count = $sth->rowCount();
        return $count;
    } catch (PDOException $e) {
        print "Error!: " . $e->getMessage() . "<br/>";
        die();
    }
}
// check SQL injection and cross site scripting
function check_injection_scripting($array) {
    $new_array = array();
    $dbh = makeConnection();
    foreach ($array as $fieldname) {
        $fieldname = htmlspecialchars($_POST[$fieldname]);
        $fieldname = $dbh->quote($fieldname);
        $new_array[] = $fieldname;
    }
    return $new_array;
}

// check empty
function check_empty($array) {
    $errors = array();
    foreach ($array as $fieldname) {
        if (!isset($_POST[$fieldname])) {
            $errors[] = "The <strong> {$fieldname} </strong> was left blank";
        }
    }
    return $errors;
}

//check_number
function check_number($field) {

    if (!is_numeric($_POST[$field])) {
        $errors = "The <strong> $field </strong> is not number";
    }
    else {
        $errors ='';
    }

    return $errors;
}

//
function check_email($email) {

    if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $_POST[$email])) {
        $errors ="The <strong> Email </strong> is wrong";
    }
    else {
        $errors='';
    }
    return $errors;
}

//check_duplicate
function check_duplicate($field, $table, $check) {
    $dbh = makeConnection();
    $execute = $dbh->query("SELECT $field FROM $table");
    while ($result = $execute->fetch(PDO::FETCH_ASSOC)) {
        if ($result[$field] == $check) {
            $errors = "This email is already taken. Please choose other sponsor name!";
        }
        else {
            $errors='';
        }
    }
    return $errors;
}

function compare_date($date1, $date2) {
    $date1 = strtotime($date1);
    $date2 = strtotime($date2);

    if ($date2 > $date1) {
        $valid = "yes";
    } else {
        $valid = "Your start date is after your end date";
    }
    return $valid;
}

?>
